<?php

declare(strict_types=1);

namespace App\Controller\delivery;
use App\Controller\BaseController;
use App\Model\AppletPlayletRel;
use App\Model\AppletPlayletRelDelivery;
use App\Model\Playlet;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use App\Model\Applet;
use App\Common\Response\SystemCode;

#[AutoController]
class AppletController  extends BaseController
{

    /**
     * 小程序列表
     */
    public function playletList(int $id,RequestInterface $request){

        if(!empty($id)) {
            $appletInfo = Applet::query()->select('id')->where('id', $id)->first();
            if (empty($appletInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '小程序不存在');
            }

            $playletIds = AppletPlayletRelDelivery::query()->where(['applet_id' => $id])->pluck('playlet_id');

            $playletData = Playlet::query()->whereIn('id',$playletIds)->select(['id as value',db::Raw('IFNULL(`alias_name`,`name`) as label'),'last_episode_number'])->get()->toArray();


            //特殊情况 2023/12/8 修改推广短剧的ID
            if(!empty($playletData)){
                foreach ($playletData as $playletDatum){
                    if($playletDatum['value'] == 37 || $playletDatum['value'] == 39){
                        $playletDatum['label'] = $playletDatum['label'].'('.$playletDatum['last_episode_number'].'集)';
                    }
                }
            }
            return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功', $playletData);

        }

        return $this->response(SystemCode::SYSTEM_ERROR, '小程序没有绑定的短剧');

    }
}